package com.hns.can.mainte.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysRoleEntity;
import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.mainte.entity.AppCarExamReportEntity;
import com.hns.can.mainte.entity.CommCarChargeInfoEntity;
import com.hns.can.mainte.entity.CommMftCopaEntity;
import com.hns.can.mainte.vo.ExamReportParamsVo;
import com.hns.can.mainte.vo.MaintenanceReportDetailVo;
import com.hns.can.mainte.vo.MaintenanceReportParamVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;

/**
 * 车辆体检报告
 * @author:yecb
 * @email:lps00@163.com
 * @version Revision 2.0.0
 */
@Service
public class ReportExaminReportService extends CommonService {
	   @Autowired
	    private FillCodeDscValue codeDscValue;
	 /**
	  * 获取上次的体检数据列表
	  * @param sysUserEntity
	  * @return
	  * @throws Exceptionw
	  * @author:yecb
	  * @email:lps00@163.com
	  */
    @SuppressWarnings("unchecked")
	@Transactional
	@LeftOrgAnnotation()
    public Pagination searchLastExaminReport(ExamReportParamsVo vo) throws Exception {
    	Pagination page = this.myBatisPage("reportExamReport.searchLastExaminReportCount", "reportExamReport.searchLastExaminReport", vo, AppCarExamReportEntity.class);
	  	Map<String,String> map = new HashMap<String,String>();
    	map.put("licPltNo",  GlobalConst.DEFAULT_CACHE_VALUE);
    	FillParamsVo fillParamsCarVo = new FillParamsVo("car_id", map);
    	map = new HashMap<String,String>();
    	map.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
    	FillParamsVo fillParamsLineVo = new FillParamsVo("line_id", map);
    	codeDscValue.fillInfoValue(page.getData(),new String[] {GlobalConst.CACHE_KEY_BASE_CAR_INFO,GlobalConst.CACHE_KEY_BASE_LINE_INFO}, fillParamsCarVo,fillParamsLineVo);
    	return page;
    }
    
	/**
	 * 手工修复故障
	 * @return
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
    @SuppressWarnings("unchecked")
 	@Transactional
    public void manualRepirFault(ExamReportParamsVo vo) throws Exception {
    	//更新故障状态
    	sqlMapDao.update("reportExamReport.manualRepirFault", vo);
    	//重新计算故障数
    	sqlMapDao.update("reportExamReport.updateMftTotalAndResolveCount", vo);
    }
    
    /**
	  * 用于导出
	  * @param vo
	  * @return
	  * @throws Exception
	  * @author:yecb
	  * @email:lps00@163.com
	  */
   @SuppressWarnings("unchecked")
	@Transactional
	@LeftOrgAnnotation()
   public List<AppCarExamReportEntity> getExamCarInfo(ExamReportParamsVo vo) throws Exception {
	   	List<AppCarExamReportEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("reportExamReport.searchLastExaminReport", vo), AppCarExamReportEntity.class);
	   	Map<String,String> map = new HashMap<String,String>();
		map.put("licPltNo",  GlobalConst.DEFAULT_CACHE_VALUE);
		FillParamsVo fillParamsCarVo = new FillParamsVo("car_id", map);
    	map = new HashMap<String,String>();
    	map.put("lineName", GlobalConst.DEFAULT_CACHE_VALUE);
    	FillParamsVo fillParamsLineVo = new FillParamsVo("line_id", map);
    	codeDscValue.fillInfoValue(list,new String[] {GlobalConst.CACHE_KEY_BASE_CAR_INFO,GlobalConst.CACHE_KEY_BASE_LINE_INFO}, fillParamsCarVo,fillParamsLineVo);
		return list;
   }
   
   /**
	  * 获取上次的体检汇总数据
	  * @param sysUserEntity
	  * @return
	  * @throws Exception
	  * @author:yecb
	  * @email:lps00@163.com
	  */
 @SuppressWarnings("unchecked")
	@Transactional
 public List<AppCarExamReportEntity> getLastAggExaminReport(SysUserEntity sysUserEntity) throws Exception {
 	MaintenanceReportParamVo conditionVo=new MaintenanceReportParamVo();
 	ArrayList<SysRoleEntity> roleIdList = new ArrayList<SysRoleEntity>(this.getCurrentUser().getSet_role());
	conditionVo.setRoleIdList(roleIdList);
 	conditionVo.setCorpId(sysUserEntity.getCorpId());
 	List<AppCarExamReportEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("reportExamReport.getLastAggExaminReport", conditionVo), AppCarExamReportEntity.class);
 	return list;
 }
   /**
    * 获取故障的分布
    * @param sysUserEntity
    * @param type
    * @return
    * @throws Exception
    * @author:yecb
    * @email:lps00@163.com
    */
   @SuppressWarnings("unchecked")
	@Transactional
  public List<CommMftCopaEntity> getMftNumList(SysUserEntity sysUserEntity,String type) throws Exception {
  	Map<String,String> conditionVo=new HashMap<String,String>();
  	conditionVo.put("corpId", sysUserEntity.getCorpId());
  	conditionVo.put("type", type);
  	List<CommMftCopaEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("reportExamReport.getMftNumList", conditionVo), CommMftCopaEntity.class);
  	return list;
  }
    
    /**
     * 车辆体检明细
     * @param conditionVo
     * @return
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @SuppressWarnings("unchecked")
	@Transactional
    public List<MaintenanceReportDetailVo> getExaminReportDetail(MaintenanceReportParamVo conditionVo) throws Exception {
    	List<MaintenanceReportDetailVo> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("reportExamReport.searchExaminReportDetail", conditionVo), MaintenanceReportDetailVo.class);
    	return list;
    }
    
    /**
     * 获取最新的体检报告值
     * @param conditionVo
     * @param request
     * @return
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @SuppressWarnings("unchecked")
	@Transactional
    public List<AppCarExamReportEntity> searchExaminReport(MaintenanceReportParamVo paramVo,SysUserEntity sysUserEntity) throws Exception {
    	MaintenanceReportParamVo conditionVo=new MaintenanceReportParamVo();
    	conditionVo.setCorpId(sysUserEntity.getCorpId());
    	conditionVo.setCarId(paramVo.getCarId());
    	List<AppCarExamReportEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("reportExamReport.searchExaminReportBYMFTALM", conditionVo), AppCarExamReportEntity.class);
        return list;
    }
    /**
     * 保存体检报告
     * @param list
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public void  saveExaminReport(List<AppCarExamReportEntity> list) throws Exception {
    	if(PubUtil.isNotEmpty(list)){
    		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    	for(AppCarExamReportEntity appCarExamReportEntity:list){
	    		appCarExamReportEntity.setExam_time(sf.format(new Date()));
	    		appCarExamReportEntity.setId(UtilString.getUUID());
	    		save(appCarExamReportEntity);
	    	}
    	}
    }
    /**
     * 删除当天的体检报告
     * @param list
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @Transactional
    public void  delExaminReport(MaintenanceReportParamVo conditionVo) throws Exception {
    	sqlMapDao.delete("reportExamReport.deleteCurDayExaminReport", conditionVo);
    }
        
    @Transactional
    public Pagination searchUnrepairAlmByCar(Map map) throws Exception {
    	Pagination page = this.myBatisPage("reportExamReport.searchUnrepairAlmByCarCount", "reportExamReport.searchUnrepairAlmByCar", map, MaintenanceReportDetailVo.class);
	  	return page;
    }
}
